package ide.ccf.ccproject.templateengine;

import ide.ccf.ccproject.common.WizardUtils;

import java.io.File;
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;


@SuppressWarnings("unused")
public class SwitchConsoleProjectData extends ProcessRunner {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void process(TemplateCore template, ProcessArgument[] args, String processId,
			IProgressMonitor monitor) throws ProcessFailureException {

			HashMap replaceMap = new HashMap();
			String projectName = args[0].getSimpleValue();
			URL path;
			try {
				path = TemplateEngineHelper.getTemplateResourceURLRelativeToTemplate(template, "/Console/console.pro");
				if (path == null) {
					throw new ProcessFailureException(getProcessMessage(processId, IStatus.ERROR, "console.pro")); //$NON-NLS-1$
				}
			} catch (IOException e1) {
				throw new ProcessFailureException(getProcessMessage(processId, IStatus.ERROR, "console.pro")); //$NON-NLS-1$
			}
			
			replaceMap.put("%PROJECT%", projectName);
			replaceMap.put("%MODULES%", "core gui xml");//mencisu TODO
			
			IProject projectHandle = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
			File file = new File(path.getFile());
			InputStream src = null;
			try {
				src = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			File dest = new File(projectHandle.getLocation().toOSString() + "/" + projectName + ".pro");
			WizardUtils.addTemplateFile(src, dest, replaceMap);
			
/*			file = new File(fPath[1] +"/templates/ConsoleProject/Console/main.cpp");
			try {
				src = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dest = new File(projectHandle.getLocation().toOSString() + "/main.cpp");
			WizardUtils.addTemplateFile(src, dest, null);	*/
			
			
	}

}
